<template>
  <view
    class="content"
    :style="{
      'padding-top': height + 'px',
    }"
  >
    <view
      :style="{
        height: statusBarHeight + 'px',
      }"
    >
    </view>
    <!-- 头部 -->
    <view
      class="head"
      :style="{
        top: statusBarHeight + 'px',
        background: isHeadBack ? 'rgba(0,0,0,0.3)' : 'rgba(0,0,0,0)',
      }"
    >
      <view class="head_left">
        <u-icon
          name="arrow-left"
          color="#EEEEEE"
          size="24"
          :bold="true"
          @click="toBack"
        ></u-icon>
      </view>
      <view class="text">兑换</view>
      <view class="head_right" @click="goPage">兑换记录</view>
    </view>

    <view class="box">
      <view class="box_item">
        <view class="box_item_title" @click="show1 = true">
          {{title1}}
          <u-icon name="arrow-down" color="#666666" size="15"></u-icon>
        </view>
        <u-line color="#666666" direction="col" length="20"></u-line>
        <u--input
          class="box_item_input"
          placeholder="请输入兑换数量"
          border="none"
          v-model="value1"
          placeholderStyle="color:#888888"
          color="#888888"
          @change=change
        ></u--input>
      </view>

      <view class="box_item_text" v-if="this.type == 'integral'">可兑换积分：{{assets.integral}}</view>
      <view class="box_item_text" v-if="this.type == 'balance'">可兑换余额：{{assets.balance}}</view>
      <view class="box_item_text" v-if="this.type == 'niule'">可兑换牛勒：{{assets.niule}}</view>

      <view class="box_item">
        <view class="box_item_title" @click="show2 = true">
            {{title2}}
          <u-icon name="arrow-down" color="#666666" size="15"></u-icon>
        </view>
        <u-line color="#666666" direction="col" length="20"></u-line>
        <u--input
          class="box_item_input"
          border="none"
          v-model="value2"
          placeholderStyle="color:#888888"
          color="#888888"
          :disabled="true"
          disabledColor="none"
        ></u--input>
      </view>

      <!-- <view class="box_item_text">100积分 > 1元人民币</view> -->
      <view class="box_item_text box_item_text1">1人民币 = {{money_to_point}}积分</view>
      <view class="box_item_text">1人民币 = {{money_to_niule}}牛勒</view>
    </view>

    <view class="bottom">
      <view class="bottom_btn" @click="exchange">兑换</view>
    </view>

    <u-action-sheet
      :actions="List1"
      cancelText="取消"
      @select="selectClick1"
      :show="show1"
      @close="show1 = false"
    ></u-action-sheet>

    <u-action-sheet
      :actions="List2"
      cancelText="取消"
      @select="selectClick2"
      :show="show2"
      @close="show2 = false"
    ></u-action-sheet>


    <floatingBall></floatingBall>
  </view>
</template>
          
          <script>
export default {
  components: {},

  data() {
    return {
      statusBarHeight: 0,
      height: 0,
      isHeadBack: false,
      value1: "",
      value2: "0.00",
      List1: [
        {
          name: "积分",
          value:'integral'
        },
        {
          name: "人民币",
          value:'balance'
        },
        {
          name: "牛勒",
          value:'niule'
        },
      ],
      List2: [
        {
          name: "积分",
          value:'integral'
        },
        {
          name: "人民币",
          value:'balance'
        },
        {
          name: "牛勒",
          value:'niule'
        },
      ],
      show1:false,
      show2:false,
      title1:'积分',
      title2:'人民币',
      type:'integral',
      to_type:'balance',
      assets:{},
      money_to_niule:'',
      money_to_point:'',
    };
  },
  created() {
    const statusBarHeight = uni.getSystemInfoSync(); // 获取手机状态栏高度

    this.statusBarHeight = statusBarHeight.statusBarHeight;
    this.$nextTick(() => {
      uni
        .createSelectorQuery()
        .select(".head")
        .boundingClientRect((data) => {
          this.height = data.height;
        })
        .exec();
    });
    this.getAssets()
  },
  methods: {
    toBack() {
      uni.navigateBack({
        delta: 1,
      });
    },
    selectClick1(a) {
      this.title1 = a.name;
      this.type = a.value
      console.log(a);
      this.show1 = false;
      if(this.value1!='' ){
        this.exchangePrice()
      }
    },
    selectClick2(a) {
      this.title2 = a.name;
      this.to_type = a.value
      console.log(a);
      this.show2 = false;
      if(this.value1!=''){
        this.exchangePrice()
      }
    },
    goPage(){
        uni.navigateTo({
            url:`/pages/integral/codeRecord?title=兑换记录&type=1`
        })
    },
    // 账户钱包余额
    getAssets(){
      this.$http
        .userAssets({ })
        .then((res) => {
          console.log(res.data.assets, "账户钱包余额");
          this.assets = res.data.assets
          this.money_to_niule = res.data.money_to_niule
          this.money_to_point = res.data.money_to_point
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    change(value){
      // console.log(value,1111111111111111)
      
      if(this.value1 == ''){
        this.value2 = '0.00'
      } else {
        this.exchangePrice()
      }
    },
    // 兑换金额
    exchangePrice(){
      this.$http
        .userExchangePrice({number:this.value1,type:this.type,to_type:this.to_type })
        .then((res) => {
          console.log(res.data.price, "兑换_价格");
          this.value2 = res.data.price
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    // 兑换操作
    exchange(){
      if(this.value1 == ''){
        return uni.$u.toast('请输入兑换数量');
      }
      this.$http
        .userExchange_action({number:this.value1,type:this.type,to_type:this.to_type })
        .then((res) => {
          console.log(res, "兑换_操作");
          uni.$u.toast(res.message);
          // this.value2 = res.data.price
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    }
    
  },
};
</script>
          
          <style scoped lang="scss">
.content {
  background: #111111;
  box-sizing: border-box;
  // background: url("/static/images/my/pic_wdjf_bwijing.png") no-repeat;
  // background-size: 100% auto;
  // background-attachment: fixed;
  z-index: -10;
  display: flex;
  flex-direction: column;
}

.head {
  width: 100%;
  padding: 18rpx 30rpx 47rpx;
  position: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  z-index: 99;
  background: #ffffff;
  .head_left {
    position: absolute;
    left: 30rpx;
  }

  .head_right {
    position: absolute;
    right: 30rpx;
    color: #55b877;
    font-size: 30rpx;
  }

  .text {
    font-size: 36rpx;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: 400;
    color: #eeeeee;
  }
}
.box {
  padding: 30rpx 60rpx;
  .box_item {
    background: #272c29;
    border-radius: 48rpx;
    padding: 26rpx 40rpx;
    display: flex;
    align-items: center;
    .box_item_title {
      font-size: 30rpx;
      color: #ffffff;
      display: flex;
      align-items: center;
      .u-icon {
        margin-left: 12rpx;
        position: relative;
        top: 3rpx;
      }
    }
    /deep/.u-line {
      margin-left: 23rpx !important;
    }
    .box_item_input {
      margin-left: 23rpx !important;
    }
  }
  .box_item_text {
    font-size: 24rpx;
    color: #adadad;
    margin-top: 24rpx;
    padding-left: 40rpx;
    margin-bottom: 56rpx;
  }
  .box_item_text1{
    margin-bottom: 0;
  }
}
.bottom {
    width: 100%;
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 0 60rpx;
  box-sizing: border-box;
  margin-bottom: 80rpx;
  .bottom_btn {
    width: 100%;
    height: 96rpx;
    background: linear-gradient(360deg, #50b674 0%, #aeddbd 100%);
    border-radius: 48rpx;
    text-align: center;
    line-height: 96rpx;
    color: #ffffff;
  }
}
</style>